TypeScript is a natural extension of JavaScript that’s used in many projects in place of JavaScript.
However, not everyone knows how it actually works.
In this article, we’ll look at how to define and use literal value types, object types, and type aliases.
Literal Value Types
A literal value type is a data type with a specific set of values and only the ones specified are allowed.
This lets us treat a set of values as a distinct type.
For instance, we can write:
let val: 1 | 2 = 1;
Then we have val
which can take on the value 1 or 2.
If we assigned something that we haven’t explicitly allow, then we get:
let val: 1 | 2 = 100;
Then we get the error ‘Type ‘100’ is not assignable to type ‘1 | 2’.ts(2322)‘
Literal Value Types in Functions
We can use literal types in functions.
This lets us restrict a parameter to allow us to set to only several kinds of values.
For instance, we can write:
const totalPrice = (quantity: 1 | 2, price: number) => {
return quantity * price;
};
Then we can only pass in 1 or 2 for quantity
.
Mixing Value Types in a Literal Value Type
We can narrow value types to a literal type.
For instance, we can write:
const randomNum = () => {
return Math.floor(Math.random() * 3) as 1 | 2 | 3;
};
Then we narrow the type of the returned value of randomNum
from number
to 1 | 2 | 3
.
randomNum
can only return 1, 2, or 3.
Overloads with Literal Value Types
We can use literal types in function overloads.
For instance, we can write:
function foo(val: 1): "foo";
function foo(val: 2): "bar" | "baz";
function foo(val: number): string {
switch (val) {
case 1: {
return "foo";
}
case 2: {
return "bar";
}
default: {
return "baz";
}
}
}
Then we can take on various values because of the overloads.
It can return the value based on the value of val
that we pass into foo
.
Type Aliases
To avoid repeatedly defining the same type, TypeScript has a type alias feature.
We can assign type combinations to an alias so we can use them throughout our code.
For instance, we can write:
type combo = 1 | 2 | 3;
The type
operator lets us define a type alias with the name we want.
In our example, our type alias name is combo
.
Objects
JavaScript objects are collections of properties that can be created using the literal syntax, or returned by constructors or classes.
Objects can be altered once they’re created.
We can add or remove properties and receive values of different types.
To provide type features for objects, TypeScript lets us specify the structure of an object.
Object Shape Type Annotations
One way to define the shape of an object is to use the object shape type annotation.
We define the property and the type of each property in the object.
For instance, we can write:
const obj: { foo: number } = { foo: 1 };
The code above defined the object obj
which can have the property foo
which is a number.
Then we assigned something that has such property and value from the right side.
How Shape Types Fit
An object must define all the properties in the shape with the given data type to match the object shape type annotation.
We’ll get an error if the property structure or the data type doesn’t match.
Optional Properties
We can make a property of an object type optional with a ?
.
For instance, we can write:
const obj: { foo: number; bar?: sting } = { foo: 1 };
Then bar
is optional and we don’t have to add a value to it.
Conclusion
We can define literal types to restrict the value of a parameter or variable to a few values.
Also, we can define object types with properties and types of them.
Finally, we can define type aliases so that we don’t have to repeat the same data type annotation everywhere.